package com.hksj.common.config.sms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
@EnableConfigurationProperties(SmsProperties.class)
public class SmsService {
    @Autowired
    private SmsProperties smsProperties;

    /**
     * 发送给指定手机号该验证码
     * @param phone 手机号,可以发送给多个用户需要将手机号使用逗号拼接
     * @param verifyCode 系统生成的验证码
     */
    public void sendVerifyCode(String phone, String verifyCode){
        String templateParas = "[" + verifyCode + "]";
        sendVerifyCode(phone,smsProperties.getDefaultTemplateId(),templateParas);
    }


    public void sendVerifyCode(String phone, String templateId,String templateParas){
        //发送短信url
        String url = "https://smsapi.cn-north-4.myhuaweicloud.com:443/sms/batchSendSms/v1";
        //国内短信通道号
        String sender = "8823081127267";
        //接收手机号 即phone
        String receiver = phone;
        //短信状态报告接收地址
        String statusCallBack = "";

        //调用SendSms的sendSms方法
        SmsUtil.sendSms(url,
                smsProperties.getAppKey(),
                smsProperties.getAppSecret(),
                sender,
                Objects.isNull(templateId) ? smsProperties.getDefaultTemplateId() : templateId,
                smsProperties.getSignature(),
                receiver,
                statusCallBack,
                templateParas
        );
    }


}
